package com.link.base.base.contract.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.link.base.base.common.dao.mybatis.mapper.AttachmentMapper;
import com.link.base.base.common.model.Attachment;
import com.link.base.base.contract.dao.mybatis.mapper.PriContItemTempMapper;
import com.link.base.base.contract.model.PriContItemTemp;
import com.link.base.base.contract.model.PriContItemTempData;
import com.link.base.base.product.service.PriContItemTempListener;
import com.link.base.core.basic.util.YmlConstants;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.model.CallParam;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 价格产品临时对象
 *
 * @date 2023-09-11 21:26:45
 */
@Service
public class PriContItemTempServiceImpl extends BasicServiceImpl<PriContItemTemp> implements PriContItemTempService {

    @Resource
    private PriContItemTempMapper    priContItemTempMapper;
    @Resource
    private AttachmentMapper attachmentMapper;

    @Override
    public BasicMapper<PriContItemTemp> getBasicMapper() {
        return priContItemTempMapper;
    }


    private static final String pattern = "^[1-9]\\d*|0$";


    @Override
    public void insertExcelData(Long attachmentId) throws Exception {
        Attachment param = new Attachment();
        param.setId(attachmentId);
        Attachment attachment = attachmentMapper.queryById(param);
        if (null == attachment.getHeadId()) {
            throw new RuntimeException("数据导入异常：项目Id丢失!");
        }
        String excelPath = YmlConstants.getPortalShareFileDir() + attachment.getAttachmentFilePath();
        ExcelReaderBuilder excelReaderBuilder =
                EasyExcel.read(excelPath,
                        PriContItemTempData.class,
                        new PriContItemTempListener(priContItemTempMapper, UserUtil.getUser(), attachment.getHeadId()));
        //获取一个工作表
        ExcelReaderSheetBuilder sheet = excelReaderBuilder.sheet();
        //读取工作表内容:sheet方法参数：工作表的顺序号（从0开始）或者工作表的名字，不传默认为0
        sheet.doRead();
    }

    @Override
    public String runDataCheck(PriContItemTemp record) throws Exception {
        // 1、先更新 物料Id
        priContItemTempMapper.updateMaterialCodeId(record);
        // 2、查询待检查的数据
        List<PriContItemTemp> list =  priContItemTempMapper.queryToCheckData(record);
        List<PriContItemTemp> checkFailList = new ArrayList<>();
        List<PriContItemTemp> checkSuccessList = new ArrayList<>();
        for (PriContItemTemp priContItemTemp : list) {
            boolean isValidateFlag = true;
            if (null == priContItemTemp.getMaterialCodeId()) {
                priContItemTemp.setErrorMsg("零件名称不存在!");
                isValidateFlag = false;
            }
            // 校验通过，则添加到校验成功队列里面
            if (isValidateFlag) {
                checkSuccessList.add(priContItemTemp);
            } else {
                checkFailList.add(priContItemTemp);
            }
        }
        // 校验失败处理
        if (!CollectionUtils.isEmpty(checkFailList)) {
            for (PriContItemTemp checkFail : checkFailList) {
                priContItemTempMapper.updateCheckFail(checkFail);
            }
        }
        // 校验成功处理
        if (!CollectionUtils.isEmpty(checkSuccessList)) {
            priContItemTempMapper.updateCheckSuccess(checkSuccessList);
        }
        return "本次校验成功：" + checkSuccessList.size() + "条，校验失败：" + checkFailList.size() + "条!";
    }

    @Override
    public CallParam runDataImport(PriContItemTemp record) throws Exception {
        if (null == record.getContractId()) {
            throw new RuntimeException("项目Id丢失，请联系管理员!");
        }
        CallParam callParam = new CallParam();
        callParam.setParam1(record.getCreatedBy());
        callParam.setParam2(String.valueOf(record.getContractId()));
        priContItemTempMapper.runDataImport(callParam);
        return callParam;
    }

    @Override
    public void deleteAll(PriContItemTemp record) throws Exception {
        priContItemTempMapper.deleteAll(record);
    }
}
